//------------------------------------------------------------------------------
// Classification: COMPANY UNCLASSIFIED
//
// Copyright (c) Terma A/S
// All rights reserved
//------------------------------------------------------------------------------

package dk.snaveponyklub.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.xml.XMLConstants;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.validation.Schema;
import javax.xml.validation.SchemaFactory;
import org.xml.sax.SAXException;

import dk.snaveponyklub.util.Logger;
import dk.snaveponyklub.xsd.Logintype;
import dk.snaveponyklub.xsd.Userlogin;

public class LoadXML {

    private Unmarshaller aUnmarshaller = null;

    private Marshaller aMarshaller = null;
    
    private boolean isInit = false;
    
    private String aLastError = "No error";

    public LoadXML() {        
    }
    
    private void init() {
        try {
            JAXBContext jc = JAXBContext.newInstance(Userlogin.class.getPackage().getName());
            aUnmarshaller = jc.createUnmarshaller();
            aMarshaller = jc.createMarshaller();
            aMarshaller.setProperty("jaxb.formatted.output", true);
            SchemaFactory schemaFactory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);

            Schema schema = null;
            URL url;
            try {
                url = new URL("http://127.0.0.1:8080/snaveponyklub/xsd/login.xsd");
                schema = schemaFactory.newSchema(url);
            } catch (MalformedURLException e) {
                Logger.error("url is null e: " + e);
            }
            
            if (schema != null) {
                aUnmarshaller.setSchema(schema);
            } else {
                Logger.error("Unable to find login.xsd");
            }
        } catch (SAXException e) {
            Logger.error("SAXException: " + e.getMessage());
        } catch (JAXBException e) {
            Logger.error("JAXBException: " + e.getMessage());
        }
    }
    
    public String getLastError() {
        return aLastError;        
    }

    public synchronized Userlogin load(InputStream file) {
        if(!isInit) {
            isInit = true;
            init();
        }
        
        Userlogin login = null;
        try {
                if (aUnmarshaller != null) {
                    login = (Userlogin) aUnmarshaller.unmarshal(file);
                } else {
                    Logger.error("Unmarshaller is null");
                }
            } catch (JAXBException e) {
                aLastError = "Xml file is not a valid xml: " + e;
                Logger.error(aLastError);
            }
        return login;
    }

    public void save(Userlogin login, String file) {
        if(!isInit) {
            isInit = true;
            init();
        }
        File output = new File(file);
        try {
            aMarshaller.marshal(login, output);
        } catch (JAXBException e) {
            Logger.error("JAXBException: " + e);
        }
    }

    public static void main(String args[]) {
        LoadXML loadXML = new LoadXML();
        /*
        Userlogin userlogin = new Userlogin();
        Logintype logintype = new Logintype();
        logintype.setUsername("host");
        logintype.setPassword("hest");        
        userlogin.setLogin(logintype);
        loadXML.save(userlogin, "login.xml");
        */
        File file = new File("login.xml");
        try {
            FileInputStream fileInputStream = new FileInputStream(file);
            
            Userlogin login = loadXML.load(fileInputStream);
            System.out.println("user: " + login.getLogin().getUsername() + " pass: " + login.getLogin().getPassword());
        } catch (FileNotFoundException e) {
            Logger.error(""+e);
        }
        
    }
}